package Jogo.States.Phases{ 
	
	import Jogo.Entities.Enemy;
	import Jogo.Entities.Tile;
	import Jogo.Entities.TurretPoint;
	import Jogo.Main;
	import Jogo.States.PhaseState;
	
	import flash.display.Bitmap;
	import flash.events.Event;
	import flash.geom.Point;
	
	public class Phase2 extends PhaseState {
		/* map */
		[Embed(source = "../../../Img/Phase1/map1.png")]
		private var mapBMP:Class;
		private var map : Bitmap;
		
		/* Where the map tile set really begins.
		* excluding HUD elements, edges, etc...
		*/
		public static const TILE_SET_BEGIN_POINT 	: Point = new Point(200,20);
		public static const TILE_SIDE			 	: int = 80;
		
		/* Number of lines (y) and number of rows (x) */
		private const MATRIX_SIZE : Point = new Point(10,9);
		
		public function Phase2() {
			tileSide = TILE_SIDE;
			
			super();
			map = new mapBMP();
			map.width = 768;
			map.height = 768;
			
			
			/* Add things to stage in the right order */
			root.addChild(map);
			root.addChild(tf);
			root.addChild(tfMap);
			root.addChild(currentPlayer);
			root.addChild(moneyTF);
			root.addChild(playersTF);
			
			TOTAL_WAVES = 10;
			
			createPhase();
			sortTurretPoints();
			currentWave++;
			wave = createWave(currentWave);
			super.callNextWave(wave);
		}
		
		override public function enterFrame(e : Event) : void{
			
			
			super.enterFrame(e);
			
			if ((enemies.length == 0) && (wave.length == 0)){
				currentWave++;
				wave = createWave(currentWave);
				super.callNextWave(wave);
			}
		}
		
		/*private function callNextWave() : void{
			currentWave++;
			wave = createWave(currentWave);
			if (currentWave >= TOTAL_WAVES){
				//ACABOU TODAS AS WAVES DA FASE
			} else {
				dispatchingEnemy = true;
				dispatchEnemyCounter = 0;
			}
		}*/
		
		/*private function dispatchEnemy() : void {
			dispatchEnemyCounter++;
			
			if(wave.length > 0){
				if (dispatchEnemyCounter > 1*Main.FRAME_RATE){
					dispatchEnemyCounter = 0;
					var enemyTemp : Enemy = wave.shift();
					enemies.push(enemyTemp);
					root.addChild(enemyTemp.getGraphicsRoot());
					enemyTemp.setInGame(true);
				}
			} else {
				dispatchingEnemy = false;
			}
		}*/
		
		private function createWave(waveId : int) : Vector.<Enemy>{
			var waveT : Vector.<Enemy> = new Vector.<Enemy>;			
			
			/* for each of the TOTAL_WAVES waves, push the enemies
			* that are on that wave
			*/
			switch (waveId){
				case 0:
					waveT.push(new Enemy(tiles[1][0], 0, 0));
					waveT.push(new Enemy(tiles[1][0], 0, 0));
					waveT.push(new Enemy(tiles[1][0], 0, 0));
					waveT.push(new Enemy(tiles[3][0], 0, 0));
					waveT.push(new Enemy(tiles[7][0], 0, 0));
					waveT.push(new Enemy(tiles[7][0], 0, 0));
					break;
				case 1:
					waveT.push(new Enemy(tiles[3][0], 0, 0));
					waveT.push(new Enemy(tiles[3][0], 0, 0));
					break;
			}
			
			return waveT;
			
		}
		
		private function createPhase() : void{
			createPhaseTiles();
			var points : Vector.<Point> = new Vector.<Point>();
			
			points.push(new Point(1,2));
			points.push(new Point(5,4));
			points.push(new Point(5,7));
			points.push(new Point(3,6));
			points.push(new Point(8,2));
			
			createTurretPoints(points);
		}
		
		
		private function createTurretPoints(tpa : Vector.<Point>) : void{
			var tp : TurretPoint;
			while (tpa.length > 0){
				
				tp = new TurretPoint(tpa.shift());
				turretPoints.push(tp);
				tp.getTurretPointMC().addEventListener(TurretPoint.TP_CLICKED, createTurret);
				root.addChild(tp.getGraphicsRoot());
			}
			
		}
		
		
		private function createPhaseTiles() : void{
			var i : int;
			var j : int;
			var pt : Point;
			
			
			tiles = new Vector.<Vector.<Tile>>(MATRIX_SIZE.y);
			for(i = 0; i < MATRIX_SIZE.y; i++) {
				tiles[i] = new Vector.<Tile>(MATRIX_SIZE.x);
				for (j=0; j < MATRIX_SIZE.x; j++){
					pt = new Point();
					pt.x = TILE_SET_BEGIN_POINT.x + TILE_SIDE/2 + j*TILE_SIDE;
					pt.y = TILE_SET_BEGIN_POINT.y + TILE_SIDE/2 + i*TILE_SIDE;
					tiles[i][j] = new Tile(pt);
				}
			}
			
			/* Populates the tiles with the paths */
			var str : String = new String();
			str =  "0,2,1,2,0,3\n0,3,0,2,1,4\n0,8,1,7,1,8\n1,0,1,1\n1,1,1,0,1,2\n1,2,1,1,0,2\n"+
				"1,4,0,3,2,5\n1,6,2,6,1,7\n1,7,1,6,0,8\n1,8,0,8,2,7\n2,3,3,3,2,4\n2,4,2,3,2,5\n"+
				"2,5,1,4,2,4,3,6\n2,6,1,6,3,6\n2,7,1,8,3,8\n2,9,3,8\n3,0,3,1\n3,1,3,0,4,1\n"+
				"3,3,4,2,4,4,2,3\n3,6,2,5,4,7,2,6\n3,8,2,7,2,9,4,8\n4,1,3,1,4,2\n4,2,4,1,3,3\n"+
				"4,4,3,3,5,3\n4,7,3,6,5,6,4,8\n4,8,4,7,3,8,4,9\n4,9,4,8,5,8\n5,3,6,2,4,4,5,4\n"+
				"5,4,5,3,5,5\n5,5,5,4,5,6,6,5\n5,6,5,5,4,7\n5,8,4,9,6,8\n6,1,7,0,7,1\n" +
				"6,2,7,1,5,3\n6,4,7,4,6,5\n6,5,6,4,5,5,7,6\n6,8,5,8,7,7\n7,0,6,1,8,1\n" +
				"7,1,6,1,6,2,7,2\n7,2,7,1,8,3\n7,4,6,4,8,3\n7,6,6,5,7,7\n7,7,7,6,6,8\n" +
				"7,8,8,7,7,9\n7,9,7,8,8,9\n8,1,7,0,8,2\n8,2,8,1,8,3\n8,3,8,2,8,4,7,2,7,4\n" +
				"8,4,8,3,8,5\n8,5,8,4,8,6\n8,6,8,5,8,7\n8,7,8,6,7,8\n8,9,7,9";

			var lineA : Array = str.split("\n");
			for(var a : int = 0; a<lineA.length; a++){
				var lineS : String = lineA[a].toString();
				var nbr : Array = lineS.split(",");
				
				var start : Point = new Point(nbr[0], nbr[1]);
				for(var b : int = 2; b<nbr.length; b=b+2){
					var pta : Point = new Point(nbr[b], nbr[b+1]);
					tiles[start.x][start.y].pathsPush(tiles[pta.x][pta.y]);
					tiles[start.x][start.y].setType(Tile.ENEMY_WAY);
				}
			}
			
			tiles[1][0].setType(Tile.START_POINT);
			tiles[3][0].setType(Tile.START_POINT);
			tiles[7][0].setType(Tile.START_POINT);
			
			tiles[2][9].setType(Tile.END_POINT);
			tiles[4][9].setType(Tile.END_POINT);
			tiles[8][9].setType(Tile.END_POINT);
			
		}
	}
}